package com.ang.juc.n5;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

@Slf4j
public class TestArray {

    private static Integer[] arr1 = new Integer[]{1, 2, 3};
    private static volatile Integer[] arr2 = new Integer[]{1, 2, 3};

    public static void main(String[] args) throws InterruptedException {
//        test1();
        test2();
    }

    //不使用volatile
    public static void test1() throws InterruptedException {
        new Thread(() -> {
            while (true){
                if(arr1[0] == 10){
                    break;
                }
            }
            System.out.println("终止");

        }).start();

        Thread.sleep(3000);
        arr1[0] = 10;
    }

    //使用volatile
    public static void test2() throws InterruptedException {
        new Thread(() -> {
            while (true){
                if(arr2[0] !=null && arr2[0] == 10){
                    break;
                }
            }
            System.out.println("终止");

        }).start();

        Thread.sleep(3000);
//        arr2 = new Integer[1];
        arr2[0] = 10;
    }

}
